<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../lib/vue.js"></script>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        #app {
            width: 600px;
            margin: 0 auto;
            text-align: center;
        }

        ul {
            list-style: none;
        }

        li {
            margin: 10px;
            border: 1px solid #333;
            border-radius: 10px;
            padding: 20px;
            /* width: 40%; */
        }
    </style>
</head>

<body>
    <div id="app">
        <h2>{{mag}}</h2>
        <div>
            <h2>展示留言</h2>
            <ul>
                <li v-for="(item,index) in list">
                    <p>序号：{{index+1}}</p>
                    <p>内容：{{item.title}}</p>
                    <p class="r">
                        <button v-on:click="list.splice(index,1)">删除</button>
                        <button v-on:click="changeItem(item,index)">修改</button>
                        <button v-on:click="likeAdd(item,index)">点赞({{item.like?item.like:0}})</button>
                    </p>
                </li>
            </ul>
            <h2>添加留言</h2>
            <div>
                <input type="text" placeholder="请输入" v-model="text">
                <button v-on:click="addToList">提交</button>
            </div>
        </div>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            mag: "Vue 简易留言板",
            list: [
                { title: "Vue 如何?", like: 0 },
                { title: "React  很简单 ?" },
                { title: "小程序很无聊" },
                { title: "原生是 基础哦 " }
            ],
            text: ""
        },
        methods: {
            changeItem(item, index) {
                var value = window.prompt(index, item.title)
                if (value) {
                    item.title = value;
                    this.list.splice(index, 1, item)  // 替换修改 
                }
            },
            likeAdd(item, index) {
                var like = item.like ? item.like : 0
                item.like = like + 1;
                this.list.splice(index, 1, item)

                // ... 展开运算符  对象或者数组展开 
                // this.list.splice(index,1,{...item,like:item.like?item.like+1:0+1})
            },
            addToList() {
                this.list.unshift({
                    title: this.text
                })
                this.text = ""
            }
        }

    })
</script>

</html>